package com.wuage.interfaces.role;

import com.wuage.application.IUacRoleService;
import com.wuage.domain.model.UacRole;
import com.wuage.infrastructure.security.LoginUser;
import com.wuage.infrastructure.utils.BeanUtils;
import com.wuage.infrastructure.utils.JwtUtils;
import com.wuage.infrastructure.utils.Response;
import com.wuage.interfaces.PagerVO;
import com.wuage.interfaces.role.domian.UacRoleDO;
import com.wuage.interfaces.role.domian.UacRolePageDO;
import com.wuage.interfaces.role.domian.UacRoleUpdateDO;
import com.wuage.interfaces.role.vo.UacRoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.bouncycastle.asn1.dvcs.CertEtcToken;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author: Cc
 * Date: 2021/9/29 9:42
 * 😀 😁 😂 😭
 */
@RestController
@RequestMapping("/role")
@Api(value = "角色相关", tags = "{角色}操作相关接口")
public class UacRoleController {

    @Resource
    private IUacRoleService uacRoleService;

    @PostMapping("/list")
    @ApiOperation("分页查询租户下角色")
    public Response<PagerVO<UacRoleVO>> getRoles(@RequestBody UacRolePageDO pageDO) {
        return Response.success(PagerVO.assembleIPager2PagerVO(uacRoleService.getRoles(pageDO, JwtUtils.getLoginUser().getTenantId()),
                (List<UacRole> list) -> BeanUtils.copyCollections(list, UacRoleVO.class)));
    }

    @PostMapping("/")
    @ApiOperation("添加角色")
    public Response<Boolean> addRole(@RequestBody @Valid UacRoleDO uacRoleDO) {
        return Response.success(uacRoleService.addRole(uacRoleDO));
    }

    @PutMapping("/")
    @ApiOperation("更新角色")
    public Response<Boolean> updateRole(@RequestBody @Valid UacRoleUpdateDO uacRoleUpdateDO) {
        return Response.success(uacRoleService.updateRole(uacRoleUpdateDO));
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除角色")
    public Response<Boolean> deleteRole(@PathVariable("id") Long id) {
        return Response.success(uacRoleService.deleteRole(id));
    }
}
